package com.mioto.ld.communication.websocket;

import com.mioto.ld.communication.websocket.util.WsChannelUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author mioto-qinxj
 * @date 2024/5/16
 * @desc
 */
@Component
@ChannelHandler.Sharable
@Slf4j
public class CloseServerHandler  extends SimpleChannelInboundHandler<CloseWebSocketFrame> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, CloseWebSocketFrame closeWebSocketFrame) throws Exception {
        log.info("client - {} close websocket connect", WsChannelUtil.getAttr(channelHandlerContext).get());
        WsChannelUtil.remove(channelHandlerContext);
        channelHandlerContext.close();
    }
}
